package com.kecheng.dao;

import com.kecheng.dto.FilterBase;
import com.kecheng.hibernate.BaseDao;
import com.kecheng.moban.PageBean;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.List;

/**
 * Hibernate万能DAO 注意：所有实体类的属性基本类型用包装类包装， 由于我们使用的jdk版本还是跟得上时代， 所以就不兼容基本类型了，为了性能！
 * 有内联接功能！没有事务功能！没有外联接功能！ 用法： 1.点出来鼠标移上去看！ 2.查完返回值要强转!
 * 
 * @author 游码农
 * @since JDK1.5
 * @version 0.1
 * 
 */
@Transactional
@Component("dao")
public class CommonDao extends BaseDao {
	private Class lei;
	@Value("${hibernate.pageSize}")
	private int pageSize;
	@Value("${hibernate.page}")
	private int page;
	DetachedCriteria criteria;

	/**
	 * 样例, dao.tp.(hibernate的模版方法)
	 */
	public HibernateTemplate tp() {
		return this.getHibernateTemplate();
	}

	/**
	 * 直接插,主键要为空
	 * 
	 * @param object
	 */
	public synchronized void add(Object object) {
		this.getHibernateTemplate().save(object);
	}
	
	/**
	 * 直接更 空值也更，在确定不会把null替换原数据的时候用，用这个不理智
	 * 
	 * @param object
	 */
	public void qupdate(Object object) {
		this.getHibernateTemplate().update(object);
	}

	/**
	 * 披量直接更 空值也更，在确定不会把null替换原数据的时候用，用这个不理智
	 * 
	 * @param list
	 */
	public void qupdate(List<?> list) {
		for (Object object : list) {
			qupdate(object);
		}
	}

	/**
	 * 局部更 空值不更
	 * 
	 * @param
	 */
	public void update(Object object) {
		DetachedCriteria criteria = DetachedCriteria
				.forClass(object.getClass());
		Object target = null;
		try {
			Field f = object.getClass().getDeclaredField("id");
			f.setAccessible(true);
			criteria.add(Restrictions.eq("id", f.get(object)));
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("更新是要有id的，亲！update");
			throw new QinException("更新是要有id的，亲！update");
		}
		List<Object> objects = (List<Object>) this.getHibernateTemplate().findByCriteria(criteria);
		if (objects.size() > 0) {
			try {
				target = objects.get(0);
				beanCopy(object, target);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			throw new QinException("用你的id找不到那个人，亲！update");
		}
		this.getHibernateTemplate().update(target);
	}

	/**
	 * 批量局部更 空值不更，应该是没人用的，为了完整性而加
	 * 
	 * @Param 一个屌样的！
	 */
	public void update(List<?> list) {
		for (Object object : list) {
			update(object);
		}
	}

	/**
	 * 局部更,不存在,保存 空值不更
	 * @param
	 */
	public void updateSave(Object object) {
		try {
			update(object);
		} catch (Exception e) {
			// TODO: handle exception
			add(object);
		}
	}

	/**
	 * 批量局部更,没的保存 空值不更，应该是没人用的，为了完整性而加
	 * 
	 * @Param 一个屌样的！
	 */
	public void updateSave(List<?> list) {
		for (Object object : list) {
			updateSave(object);
		}
	}

	/**
	 * 查出所有出页数
	 * 
	 * @param
	 * @return 页数
	 */
	public Integer findPage(Class c) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(c);
		Integer count = getHibernateTemplate().findByCriteria(criteria).size();
		return computedAllPage(count);
	}

	private Integer computedAllPage(Integer count) {
		int countint = count;
		if (countint % pageSize == 0) {
			if (countint < pageSize) {
				return 1;
			} else {
				return countint / pageSize;
			}
		} else {
			return countint / pageSize + 1;
		}
	}

	/************************** 查出所有 ************************/
	/**
	 * 查出所有,默认id降序
	 * 
	 * @param
	 * @return List<Object>
	 */
	public List<?> find(Class c) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(c);
		criteria.addOrder(Order.asc("id"));
		List<Object> objects = (List<Object>) this.getHibernateTemplate().findByCriteria(
				criteria);
		return objects;
	}
	
	public List<?> searchLike(Class c,String keyword,String property1,String property2) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(c);
		if(c.getName().toString().equals("pojo.Message")){
			criteria.add(Restrictions.eq("status", 1));//产品
			criteria.add(Restrictions.eq("style", "供货区"));
		}else{
			criteria.add(Restrictions.eq("status", 2));//商家
		}
		criteria.add(Restrictions.or(Restrictions.like(property1, keyword,
				MatchMode.ANYWHERE), Restrictions.like(property2, keyword,
						MatchMode.ANYWHERE)));
		criteria.addOrder(Order.desc("id"));
		List<Object> objects = (List<Object>) this.getHibernateTemplate().findByCriteria(
				criteria);
		return objects;
	}

	/**
	 * 查出所有，默认ID降序，分页
	 */
	public List<?> find(Class c, Integer page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(c);
		criteria.addOrder(Order.desc("id"));
		List<Object> objects = (List<Object>) this.getHibernateTemplate().findByCriteria(
				criteria, (page - 1) * pageSize, pageSize);
		return objects;
	}

	/**
	 * 按照指定查出所有,指定降序字段,未做异常处理，查不了，自己对字段
	 * 
	 * @param
	 * @return List<Object>
	 */
	public Object findByAttrOrder(Object o, String name,String order) {
		DetachedCriteria criteria = DetachedCriteria.forClass(o.getClass());
		Field f;
		try {
			f = o.getClass().getDeclaredField(name);
			f.setAccessible(true);
			try {
				criteria.add(Restrictions.eq(name, f.get(o)));
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		criteria.addOrder(Order.desc(order));
		List<Object> objects = (List<Object>) this.getHibernateTemplate().findByCriteria(
				criteria);
		return objects;
	}
	/**
	 * 查出所有,指定降序字段,未做异常处理，查不了，自己对字段
	 * 
	 * @param
	 * @return List<Object>
	 */
	public List<?> find(Class c, String order) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(c);
		criteria.addOrder(Order.desc(order));
		List<Object> objects = (List<Object>) this.getHibernateTemplate().findByCriteria(criteria);
		return objects;
	}

	/**
	 * 查出所有,指定降序字段,未做异常处理，查不了，自己对字段
	 * 
	 * @param
	 * @page 页码
	 * @return List<Object>
	 */
	public List<?> find(Class c, String order, Integer page) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(c);
		criteria.addOrder(Order.desc("order"));
		List<Object> objects = (List<Object>) this.getHibernateTemplate().findByCriteria(
				criteria, (page - 1) * pageSize, pageSize);
		return objects;
	}

	/**
	 * 查出所有,指定升序字段，未做异常处理，查不了，自己对order字段名
	 * @param
	 * @return List<Object>
	 */
	public List<?> find(String order, Class c) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(c);
		criteria.addOrder(Order.desc("order"));
		List<Object> objects = (List<Object>) this.getHibernateTemplate().findByCriteria(
				criteria);
		return objects;
	}

	/**
	 * 查出所有,指定升序字段，未做异常处理，查不了，自己对order字段名
	 * 
	 * @param c
	 *            如Team.class
	 * @return List<Object>
	 */
	public List<?> find(String order, Class c, Integer page) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(c);
		criteria.addOrder(Order.desc("order"));
		List<Object> objects = (List<Object>) this.getHibernateTemplate().findByCriteria(
				criteria, (page - 1) * pageSize, pageSize);
		return objects;
	}

	/************************* 指定条件查询 *************************/
	/**
	 * 多条件查询所有，无分页
	 * 
	 * @param o
	 *            丢入的对象有多个参数，模糊查询，取交集
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws ClassNotFoundException
	 */
	public List<?> find(Object o) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(o.getClass());
		// Class c=Order2.class;
		addCriteria(o, criteria);
		criteria.addOrder(Order.asc("id"));
		List<Object> objects = (List<Object>) this.getHibernateTemplate().findByCriteria(
				criteria);
		return objects;
	}
	public List<?> findDesc(Object o,String order) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(o.getClass());
		// Class c=Order2.class;
		addCriteria(o, criteria);
		criteria.addOrder(Order.desc(order));
		List<Object> objects = (List<Object>) this.getHibernateTemplate().findByCriteria(
				criteria);
		return objects;
	}


	/**
	 * 多条件查询，总页码
	 */
	public Integer findPage(Object o) {
		DetachedCriteria criteria = DetachedCriteria.forClass(o.getClass());
		// Class c=Order2.class;
		addCriteria(o, criteria);
		criteria.addOrder(Order.desc("id"));
		Integer count = this.getHibernateTemplate().findByCriteria(criteria)
				.size();
		return computedAllPage(count);
	}

	/**
	 * 多条件非空判定
	 * 
	 * @param o
	 * @param criteria
	 */
	private void addCriteria(Object o, DetachedCriteria criteria) {
		Field[] fileds = o.getClass().getDeclaredFields();
		System.out.println("cd" + fileds.length);
		for (Field f : fileds) {
			f.setAccessible(true);
			try {
				if (f.getModifiers()!=26&&f.getModifiers()!=25&&f.get(o) != null && !f.get(o) .equals("")) {
					if (f.getType() == Integer.class
							|| f.getType() == Short.class
							|| f.getType() == Long.class
							|| f.getType() == Double.class
							|| f.getType() == Float.class
							|| f.getType() == Timestamp.class) {
						//此处进行了一个很危险的修
						criteria.add(Restrictions.like(f.getName(), f.get(o)));
						//System.out.println("dao:1" + f.getName() + f.get(o));
					} else {
						criteria.add(Restrictions.like(f.getName(), f.get(o)
								.toString(), MatchMode.ANYWHERE));
						//System.out.println("dao:2" + f.getName() + f.get(o)+ f.getType());
					}
				}
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// System.out.println(f.getName());
		}
	}

	/**
	 * 多条件分页查询，分页
	 * 
	 * @param o
	 *            条件对象
	 * @param page
	 *            不用说你也知道
	 */
	public List<?> find(Object o, Integer page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(o.getClass());
		// Class c=Order2.class;
		addCriteria(o, criteria);
		criteria.addOrder(Order.desc("id"));
		List<Object> objects = (List<Object>) this.getHibernateTemplate().findByCriteria(
				criteria, (page - 1) * pageSize, pageSize);
		// System.out.println("page:"+page);
		// System.out.println("objects:"+objects.size());
		return objects;
	}

	/**
	 * 多条件分页查询，分页，指定降序
	 * 
	 * @param o
	 *            条件对象
	 * @param page
	 *            不用说你也知道
	 */
	public List<?> find(Object o, String order, Integer page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(o.getClass());
		// Class c=Order2.class;
		addCriteria(o, criteria);
		criteria.addOrder(Order.desc(order));
		List<Object> objects = (List<Object>) this.getHibernateTemplate().findByCriteria(
				criteria, (page - 1) * pageSize, pageSize);
		return objects;
	}

	/**
	 * 我也不懂怎么写了,并集查询,组ID查询,先顶着用
	 */
	public List<?> findids(Class c,List<Integer> ids, String order) {
		if (ids == null || ids.size() < 1) {
			//throw new QinException("ids集合有问题,为空或是别的");
			return null;
		}
		DetachedCriteria criteria = DetachedCriteria.forClass(c);
		criteria.add(Restrictions.in("id", ids));
		//criteria.addOrder(Order.desc(order));
		List<Object> objects = (List<Object>) this.getHibernateTemplate().findByCriteria(
				criteria);
		return objects;
	}


	/**
	 * 多条件分页查询，分页，指定升序
	 * 
	 * @param o
	 *            条件对象
	 * @param page
	 *            不用说你也知道
	 */
	public List<?> find(String order, Object o, Integer page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(o.getClass());
		// Class c=Order2.class;
		// Field[] fileds = o.getClass().getDeclaredFields();
		// System.out.println("cd" + fileds.length);
		addCriteria(o, criteria);
		criteria.addOrder(Order.asc(order));
		List<? extends List<?>> objects = Collections.singletonList(this.getHibernateTemplate().findByCriteria(
				criteria, (page - 1) * pageSize, pageSize));
		return objects;
	}

	/**
	 * 多条件查询，可以用数字区间查询
	 * 
	 * @prama obj1 与obj2是同一个对象，数字属性的左值及字符串属性，set到这个类里面
	 * @prama obj2 数字的右值，别的属性无效，
	 */
	public List<?> find(Object obj1, Object obj2) {
		DetachedCriteria criteria = DetachedCriteria.forClass(obj1.getClass());
		// Class c=Order2.class;
		addCriteriaIncludeBetween(obj1, obj2, criteria);
		criteria.addOrder(Order.asc("id"));
		List<Object> objects = (List<Object>) this.getHibernateTemplate().findByCriteria(
				criteria);
		return objects;
	}

	/**
	 * 多条件查询，可以用数字区间查询,分页
	 * 
	 * @prama obj1 与obj2是同一个对象，数字属性的左值及字符串属性，set到这个类里面
	 * @prama obj2 数字的右值，别的属性无效，
	 */
	public List<?> find(Object obj1, Object obj2, Integer page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(obj1.getClass());
		// Class c=Order2.class;
		addCriteriaIncludeBetween(obj1, obj2, criteria);
		criteria.addOrder(Order.asc("id"));
		List<Object> objects = (List<Object>) this.getHibernateTemplate().findByCriteria(
				criteria, (page - 1) * pageSize, pageSize);
		return objects;
	}

	/**
	 * 多条件查询，可以用数字区间查询,分页,指定排序
	 * 
	 * @prama obj1 与obj2是同一个对象，数字属性的左值及字符串属性，set到这个类里面
	 * @prama obj2 数字的右值，别的属性无效，
	 */
	public List<?> find(Object obj1, Object obj2, String order, Integer page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(obj1.getClass());
		// Class c=Order2.class;
		addCriteriaIncludeBetween(obj1, obj2, criteria);
		criteria.addOrder(Order.asc(order));
		List<Object> objects = (List<Object>) this.getHibernateTemplate().findByCriteria(
				criteria, (page - 1) * pageSize, pageSize);
		return objects;
	}

	private void addCriteriaIncludeBetween(Object obj1, Object obj2,
			DetachedCriteria criteria) {
		Field[] fileds = obj1.getClass().getDeclaredFields();
		System.out.println("cd" + fileds.length);
		for (Field f : fileds) {
			f.setAccessible(true);
			try {
				if (f.get(obj1) != null && f.get(obj1) != "") {
					if (f.getType() == Integer.class
							|| f.getType() == Short.class
							|| f.getType() == Long.class
							|| f.getType() == Double.class
							|| f.getType() == Float.class
							|| f.getType() == Timestamp.class) {
						if (f.get(obj2) != null && f.get(obj2) != "") {
							criteria.add(Restrictions.between(f.getName(),
									f.get(obj1), f.get(obj2)));
						} else {
							criteria.add(Restrictions.like(f.getName(), f
									.get(obj1)));
							System.out.println("dao:1" + f.getName()
									+ f.get(obj1));
						}
					} else {
						if (f.get(obj2) != null && f.get(obj2) != "") {
							criteria.add(Restrictions.between(f.getName(),
									f.get(obj1), f.get(obj2)));
						} else {
							criteria.add(Restrictions.like(f.getName(), f.get(obj1)
									.toString(), MatchMode.ANYWHERE));
							System.out.println("dao:2" + f.getName() + f.get(obj1)
									+ f.getType());
						}
					}
				}
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * 多条件页数查询,区间查询，总页数
	 */
	public Integer findPage(Object obj1, Object obj2) {
		DetachedCriteria criteria = DetachedCriteria.forClass(obj1.getClass());
		// Class c=Order2.class;
		addCriteriaIncludeBetween(obj1, obj2, criteria);
		Integer count = this.getHibernateTemplate().findByCriteria(criteria)
				.size();
		return computedAllPage(count);
	}

	/**
	 * 指定字段查询,用于orderid之类的查询
	 */
	public Object findOne(Object o, String name) {
		DetachedCriteria criteria = DetachedCriteria.forClass(o.getClass());
		Field f;
		try {
			f = o.getClass().getDeclaredField(name);
			f.setAccessible(true);
			try {
				criteria.add(Restrictions.eq(name, f.get(o)));
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<Object> objects = (List<Object>) this.getHibernateTemplate().findByCriteria(
				criteria);
		if (objects.size() > 0) {
			return objects.get(0);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public Object findOne(Object o) {
		Object object = null;
		DetachedCriteria criteria = DetachedCriteria.forClass(o.getClass());
		try {
			Field f = o.getClass().getDeclaredField("id");
			f.setAccessible(true);
			criteria.add(Restrictions.eq("id", f.get(o)));
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<Object> objects = (List<Object>) this.getHibernateTemplate().findByCriteria(
				criteria);
		if (objects.size() > 0) {
			return objects.get(0);
		}
		return null;
	}

	/**
	 * 指定字段查询多条件,用于orderid之类的查询
	 */
	public Object findOne(Object o, String[] names) {
		Object object = null;
		DetachedCriteria criteria = DetachedCriteria.forClass(o.getClass());
		Class c = o.getClass();
		for (int i = 0; i < names.length; i++) {
			try {
				Field f = c.getDeclaredField(names[i]);
				f.setAccessible(true);
				criteria.add(Restrictions.eq(names[i], f.get(o)));
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		List<Object> objects = (List<Object>) this.getHibernateTemplate().findByCriteria(
				criteria);
		if (objects.size() > 0) {
			return objects.get(0);
		}
		return null;
	}

	// /**
	// * 指定字段查询,用于orderid之类的查询
	// */
	// public List<?> find(Object o,String name){
	// DetachedCriteria criteria = DetachedCriteria.forClass(o.getClass());
	// criteria.add(Restrictions.eq(name, o));
	// List<Object>
	// objects=this.getHibernateTemplate().findByCriteria(criteria);
	// return objects;
	// }
	/**
	 * 指定字段查询多条件,用于orderid之类的查询
	 */
	// public List<?> find(Object o,String[] names){
	// Object object=null;
	// DetachedCriteria criteria = DetachedCriteria.forClass(o.getClass());
	// for(int i=0;i<names.length;i++){
	// criteria.add(Restrictions.eq(names[i], o));
	// }
	// List<Object>
	// objects=this.getHibernateTemplate().findByCriteria(criteria);
	// return objects;
	// }
	//	
	/*************************** 删除，没毛东西 ************************/
	/**
	 * 就是一个按ID删除，和原来一个屌样
	 * 
	 * @param
	 */
	public void delete(Object object) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().delete(this.findOne(object));
	}

	/**
	 * 按ID批量删除,点有脑残吧
	 */
	public void dedete(List<?> list) {
			for (Object object : list) {
				delete(object);
		}
	}

	/**
	 * 内关联一对多,,先不写了可外部实现
	 */
	public void findParentid() {

	}

	/**
	 * 内关联多对多
	 */

	/******************** 指定类型后的方法 ****************************/
	/**
	 * 指定类型后查出所有页数，先不写了，体力活
	 */
	public Integer findPage() {
		return null;
	}

	/**
	 * 指定类型后按ID删除
	 * 
	 * @param id
	 *            呵呵
	 * @return
	 */
	public void delete(Integer id) {

	}

	/**
	 * 指定类型后批量删除
	 * 
	 * @return
	 */
	public void delete(List<Integer> ids) {
		
	}

	/**
	 * 指定类型后单条件查询，这个不写有病
	 * 
	 * @return
	 */
	public List<?> find(String parameter) {
		return null;
	}

	/**
	 * 指定类型后多条件查询，这个不写有病
	 * 
	 * @return
	 */
	public List<?> find(List<String> parameters) {
		return null;
	}

	/************** 传入hql查询 **************************/
	/**
	 * 用传入hql查询
	 * 
	 * @return
	 */
	public List<?> hqlfind(String hql) {
		List<Object> objects = (List<Object>) this.getHibernateTemplate().find(hql);
		return objects;
	}

	public PageBean<?> hqlFindPageBean(String sql, String countSql,FilterBase filterBase,Class c) {
		PageBean<Object> pageBean = new PageBean<>();
		if(filterBase!=null){
			pageBean.setPage(filterBase.getPage());
			pageBean.setPageSize(filterBase.getPageSize());
		}else{
			pageBean.setPage(page);
			pageBean.setPageSize(pageSize);
		}
		Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession();
		Query query= session.createSQLQuery(sql).addEntity(c);
		Integer count = query.list().size();

		query.setFirstResult((pageBean.getPage()-1)*pageBean.getPageSize());//就是说你的从第几条开始
		query.setMaxResults(pageBean.getPageSize());//页面默认显示的条数
		List<Object> objects = query.list();

		pageBean.setData(objects);
		pageBean.setTotal(count);
		pageBean.setTotalPage(computedAllPage(count));
		return pageBean;

	}

	/**
	 * 传入hql查询，有param
	 * 
	 * @return
	 */
	public List<?> hqlfind(String hql, String[] parameters) {
		List<Object> objects = (List<Object>) this.getHibernateTemplate()
				.find(hql, parameters);
		return null;
	}

	/**
	 * 获取每页行数！
	 * 
	 * @return
	 */
	public int getRowsPerPage() {
		return pageSize;
	}

	/**
	 * 设置每页行数，不设就是20
	 * 
	 * @return
	 */
	public void setRowsPerPage(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * 获取当前指定类
	 * 
	 * @return
	 */
	public Class getLei() {
		return lei;
	}

	/**
	 * 设置当前指定类
	 * 
	 * @param lei
	 *            如Team.class
	 */
	public void setLei(Class lei) {
		this.lei = lei;
		criteria = DetachedCriteria.forClass(lei);
	}

	/*
	 * 公具方法，
	 */
	private void beanCopy(Object source, Object target) throws Exception {
		if (source == null || target == null) {
			throw new Exception("param is null.");
		}
		Field sourceField[] = source.getClass().getDeclaredFields();
		Field targetField[] = target.getClass().getDeclaredFields();
		if (sourceField == null || sourceField.length == 0) {
			throw new Exception("Source bean no properties.");
		}
		if (targetField == null || targetField.length == 0) {
			throw new Exception("Target bean no properties.");
		}
		for (Field tf : targetField) {
			tf.setAccessible(true);
			//tf.getModifiers()==?获取属性的修饰符
			//private static输出26、public static输出25
			if (tf.getModifiers()!=26&&tf.getModifiers()!=25&&tf.get(source) != null) {
				tf.set(target, tf.get(source));
			}
		}
	}

	/************* 异常内部类 **************/
	private class QinException extends RuntimeException {
		public QinException() {
			super();
		}

		public QinException(String msg) {
			super(msg);
		}
	}

	/***************** 以下是测试方法 *************/
	@Test
	public void test() {
		// 抛异常
		try {
			throw new QinException("出异常了");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/************************************新加的分页**********************/
	/**
	 * 查出所有，默认ID降序，分页
	 */
	public PageBean<?> findPageBean(Class c, PageBean<?> pageBeanObject) {
		PageBean<Object> pageBean = (PageBean<Object>) pageBeanObject;
		if(pageBean==null){
			pageBean = new PageBean<>();
			pageBean.setPage(page);
		}
		pageBean.setPageSize(pageSize);

		DetachedCriteria criteria = DetachedCriteria.forClass(c);
		criteria.addOrder(Order.desc("id"));
		List<Object> objects = (List<Object>) this.getHibernateTemplate().findByCriteria(
				criteria, (pageBean.getPage() - 1) * pageBean.getPageSize(), pageBean.getPageSize());

		String hql = "select count(*) from "+c.getName();
		Integer count = ((Long)getHibernateTemplate().find(hql).listIterator().next()).intValue();

		pageBean.setData(objects);
		pageBean.setTotal(count);
		pageBean.setTotalPage(computedAllPage(count));
		return pageBean;

	}

	/**
	 * 根据条件查出所有，默认ID降序，分页
	 */
	public PageBean<?> findPageBeanByObj(Object o, PageBean<?> pageBeanObject) {
		PageBean<Object> pageBean = (PageBean<Object>) pageBeanObject;
		if(pageBean==null){
			pageBean = new PageBean<>();
			pageBean.setPage(page);
		}
		pageBean.setPageSize(pageSize);

		DetachedCriteria criteria = DetachedCriteria.forClass(o.getClass());
		addCriteria(o, criteria);
		criteria.addOrder(Order.desc("id"));

		List<Object> objects = (List<Object>) this.getHibernateTemplate().findByCriteria(
				criteria, (pageBean.getPage() - 1) * pageBean.getPageSize(), pageBean.getPageSize());

		String hql = "select count(*) from "+o.getClass().getName();
		Integer count = ((Long)getHibernateTemplate().find(hql).listIterator().next()).intValue();

		pageBean.setData(objects);
		pageBean.setTotal(count);
		pageBean.setTotalPage(computedAllPage(count));
		return pageBean;

	}

}
